package com.kcht.np.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kcht.np.common.exception.ParameterException;
import com.kcht.np.entity.SysDictType;
import com.kcht.np.mapper.SysDictTypeMapper;
import com.kcht.np.service.SysDictTypeService;
import com.kcht.np.vo.dict.DictTypeCanCreateInfo;
import com.kcht.np.vo.dict.DictTypeCanModifyInfo;
import com.kcht.np.vo.page.ConditionsQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

@Slf4j
@Service
@DS("kchtnp")
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService {
    private final String DICT_TYPE ="dict_type";
    private final String DICT_NAME ="dict_name";
    private final String DICT_STATUS ="status";
    @Override
    public SysDictType getById(Serializable id) {
        SysDictType dictType = super.getById(id);
        if(dictType==null){
            throw new ParameterException("无此标识为<"+id+">的字典类型");
        }
        return dictType;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(DictTypeCanCreateInfo newDictType) {
        SysDictType dictType=new SysDictType();
        BeanUtils.copyProperties(newDictType,dictType);
        verifyParam(dictType);
        if(dictType.getComment()==null){
            dictType.setComment("暂无描述");
        }
        super.save(dictType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DictTypeCanModifyInfo modifyDictType) {
        SysDictType dictType = super.getById(modifyDictType.getId());
        if(dictType==null){
            throw new ParameterException("无此标识为<"+modifyDictType.getId()+">的字典类型");
        }
        if(!dictType.getDictType().equals(modifyDictType.getDictType())){
            int count = super.count(new QueryWrapper<SysDictType>().eq(DICT_TYPE,modifyDictType.getDictType()));
            if(count>0){
                throw new ParameterException("已存在"+modifyDictType.getDictType()+"的字典类型");
            }
        }
        BeanUtils.copyProperties(modifyDictType,dictType);
        super.saveOrUpdate(dictType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        SysDictType dictType = super.getById(id);
        if(dictType==null){
            throw new ParameterException("无此标识为<"+id+">的字典类型");
        }
        super.removeById(id);
    }

    private void verifyParam(SysDictType param){
        int count = super.count(new QueryWrapper<SysDictType>().eq(DICT_TYPE,param.getDictType()));
        if(count>0){
            throw new ParameterException("已存在"+param.getDictType()+"的字典类型");
        }
    }

    @Override
    public IPage<SysDictType> page(ConditionsQuery parameter) {
        String dictType = parameter.getConditions().get(DICT_TYPE);
        String dictName=parameter.getConditions().get(DICT_NAME);
        String dictStatus=parameter.getConditions().get(DICT_STATUS);
        IPage<SysDictType> page = new Page<>(parameter.getPageNum(), parameter.getPageSize());
        QueryWrapper<SysDictType> queryModel = new QueryWrapper<SysDictType>();
        if(StringUtils.isNotEmpty(dictStatus)){
            queryModel=queryModel.eq(DICT_STATUS,dictStatus);
        }
        if(StringUtils.isNotEmpty(dictType)){
            queryModel=queryModel.like(DICT_TYPE,dictType);
        }
        if(StringUtils.isNotEmpty(dictName)){
            queryModel=queryModel.like(DICT_NAME,dictName);
        }
        return super.page(page,queryModel);
    }

}
